﻿using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    /// <summary>
    /// 对ExtractFeature表进行操作
    /// </summary>
    public class ExtractFeatureDAL
    {
        private string DbName = "LdtCommonManage";
     
        /// <summary>
        /// 获取节点配置信息
        /// </summary>
        /// <param name="nodeName">段落编号或者节点XmlPath</param>
        /// <param name="nodeType">0代表段落1代表要素</param>
        /// <returns>配置信息数据</returns>
        public DataTable GetExtractFeatureData(string nodeName,Int32 nodeType)
        {
            string sql = string.Empty;

            if (nodeType == 0)
            {
                sql = string.Format(" select 'false' as Checked,'是'as IsConfig,a.EXI_ID,b.EF_ID, a.EXI_ParagraCode as nodeName,a.EXI_ModuleCode,b.EF_FeatureName,b.EES_ID,b.RIU_ID,b.CM_ID,b.RM_ID from  dbo.ExtractInfo a join dbo.ExtractFeature b on a.EXI_ID=b.EXI_ID  where EXI_ParagraCode='{0}' and EXI_DataMode=2 ", nodeName);
            }
            else if (nodeType == 1)
            {
                sql = string.Format(" select 'false' as Checked,'是'as IsConfig,a.EXI_ID,b.EF_ID, a.EXI_XmlPath as nodeName,a.EXI_ModuleCode,b.EF_FeatureName,b.EES_ID,b.RIU_ID,b.CM_ID,b.RM_ID  from dbo.ExtractInfo a join dbo.ExtractFeature b on a.EXI_ID=b.EXI_ID  where EXI_XmlPath='{0}' and EXI_DataMode=1 ", nodeName);
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取节点配置信息
        /// </summary>
        /// <param name="nodeName">段落编号或者节点XmlPath</param>
        /// <param name="nodeType">0代表段落1代表要素</param>
        /// <param name="tuples">要素内容屬性</param>
        /// <returns>配置信息数据</returns>
        public DataTable GetExtractFeatureData(string nodeName, Int32 nodeType, Tuple<string, string> tuples)
        {
            string sql = string.Empty;

            if (nodeType == 0)
            {
                sql = string.Format(" select 'false' as Checked,'是'as IsConfig,a.EXI_ID,b.EF_ID, a.EXI_ParagraCode as nodeName,a.EXI_ModuleCode,b.EF_FeatureName,b.EES_ID,b.RIU_ID,b.CM_ID,b.RM_ID from  dbo.ExtractInfo a join dbo.ExtractFeature b on a.EXI_ID=b.EXI_ID  where EXI_ParagraCode='{0}' and EXI_DataMode=2 and a.EXI_ModuleCode = '{1}' and b.EF_FeatureName = '{2}' ", nodeName, tuples.Item1, tuples.Item2);
            }
            else if (nodeType == 1)
            {
                //sql = string.Format(" select 'false' as Checked,'是'as IsConfig,a.EXI_ID,b.EF_ID, a.EXI_XmlPath as nodeName,a.EXI_ModuleCode,b.EF_FeatureName,b.EES_ID,b.RIU_ID,b.CM_ID,b.RM_ID  from dbo.ExtractInfo a join dbo.ExtractFeature b on a.EXI_ID=b.EXI_ID  where EXI_XmlPath='{0}' and EXI_DataMode=1 ", nodeName);
                sql = string.Format(@" select 'false' as Checked,'是'as IsConfig,a.EXI_ID,b.EF_ID, a.EXI_XmlPath as nodeName,
                                        a.EXI_ModuleCode, b.EF_FeatureName, b.EES_ID, b.RIU_ID, b.CM_ID, b.RM_ID  from
                                        dbo.ExtractInfo a join dbo.ExtractFeature b on a.EXI_ID = b.EXI_ID  where EXI_XmlPath = '{0}' and EXI_DataMode = 1 and a.EXI_ModuleCode = '{1}' and b.EF_FeatureName = '{2}' ", nodeName, tuples.Item1, tuples.Item2);
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取节点配置信息
        /// </summary>
        /// <param name="nodeName">段落编号或者节点XmlPath</param>
        /// <param name="nodeType">0代表段落1代表要素</param>
        /// <param name="moduleCode">模块名称</param>
        /// <returns>配置信息数据</returns>
        public DataTable GetExtractFeatureData(string nodeName, Int32 nodeType, string moduleCode)
        {
            string sql = string.Empty;

            if (nodeType == 0)
            {
                sql = string.Format(@"   select *
  from dbo.ExtractInfo a join dbo.ExtractFeature b on a.EXI_ID = b.EXI_ID
  where EXI_ParagraCode = '{0}' and EXI_DataMode = 2 and b.EF_ModuleCode = '{1}' ", nodeName, moduleCode);
            }
            else if (nodeType == 1)
            {
                sql = string.Format(@" select *  from
                                        dbo.ExtractInfo a join dbo.ExtractFeature b on a.EXI_ID = b.EXI_ID 
where EXI_XmlPath = '{0}' and EXI_DataMode = 1 and b.EF_ModuleCode = '{1}' ", nodeName, moduleCode);
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 增加ExtractFeature表数据
        /// </summary>
        public bool ExtractFeatureAdd(ExtractFeature model)
        {
            string strSql = string.Format(@"insert into ExtractFeature(EF_ID, EXI_ID, EF_ModuleCode, EES_ID, RIU_ID, CM_ID,RM_ID,EF_Remark,EF_UpdateTime) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')",
                model.EF_ID,model.EXI_ID,model.EF_ModuleCode,model.EES_ID,model.RIU_ID,model.CM_ID,model.RM_ID,model.EF_Remark,DateTime.Now);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());            
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 批量添加前删除表数据
        /// </summary>
        /// <param name="exiID"></param>
        /// <param name="ModuleCode"></param>
        /// <returns></returns>
        public bool DeleteExtractFeature(string exiID, string ModuleCode)
        {
            string strSql = string.Format(@" delete from ExtractFeature where EXI_ID='{0}' and EF_ModuleCode= '{1}' ",
                exiID, ModuleCode);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 增加ExtractInfo表数据
        /// </summary>
        public bool ExtractInfoAdd(ExtractInfo model)
        {
            //先判断该条数据是否存在，存在则不进行添加，否则进行插入
            if (model.EXI_DataMode == 1)
            {
                string exiID = GetExtractInfoData(model.EXI_XmlPath, 1);
                if (model.EXI_ID == exiID)
                {
                    return true;
                }
            }else if (model.EXI_DataMode == 2)
            {
                string exiID = GetExtractInfoData(model.EXI_ParagraCode, 2);
                if (model.EXI_ID == exiID)
                {
                    return true;
                }
            }
            string strSql = string.Format(@"insert into ExtractInfo(EXI_ID, EXI_XmlPath, EXI_XmlCode, EXI_ParagraCode,  EXI_DataMode,EXI_DefineID,EXI_Remark,EXI_UpdateTime) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",
                model.EXI_ID, model.EXI_XmlPath, model.EXI_XmlCode, model.EXI_ParagraCode,  model.EXI_DataMode, model.EXI_DefineID, model.EXI_Remark, DateTime.Now);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///     取出本次数据总数
        /// </summary>
        /// <param name="headWords"></param>
        /// <returns></returns>
        public int GetIsPublic(ExtractFeature extractFeatureModel)
        {
            string sql = string.Format(@" select count(1)from dbo.ExtractFeature where EES_ID='{0}' and RIU_ID='{1}' and CM_ID='{2}'and RM_ID='{3}' ", extractFeatureModel.EES_ID, extractFeatureModel.RIU_ID,extractFeatureModel.CM_ID,extractFeatureModel.RM_ID);
            var oBase = DatabaseFactory.CreateDatabase(DbName);
            var oCmd = oBase.GetSqlStringCommand(sql.ToString());

            return Convert.ToInt32(oBase.ExecuteScalar(oCmd));
        }

        /// <summary>
        /// 删除ExtractFeature表数据
        /// </summary>
        public bool ExtractFeatureDelete(string efId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ExtractFeature ");
            strSql.Append(" where EF_ID=@EF_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@EF_ID", DbType.String, efId);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除ExtractInfo表数据
        /// </summary>
        public bool ExtractInfoDelete(string exiId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ExtractInfo ");
            strSql.Append(" where EXI_ID=@EXI_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@EXI_ID", DbType.String, exiId);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改ExtractFeature表数据
        /// </summary>
        public bool ExtractFeatureUpdate(ExtractFeature model)
        {
            string sql = string.Format(@" update ExtractFeature set  EES_ID='{0}', RIU_ID='{1}', CM_ID='{2}',RM_ID='{3}',EF_UpdateTime='{4}' where EF_ID='{5}' ",
               model.EES_ID,model.RIU_ID,model.CM_ID,model.RM_ID,DateTime.Now, model.EF_ID );
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);        
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改ExtractFeature表数据
        /// </summary>
        public bool ExtractFeatureUpdateCMID(ExtractFeature model)
        {
            string sql = string.Format(@" update ExtractFeature set  CM_ID='{0}',EF_UpdateTime='{1}' where EF_ID='{2}' ",
               model.CM_ID, DateTime.Now, model.EF_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }



        /// <summary>
        /// 获取节点配置信息
        /// </summary>
        /// <param name="nodeName">段落编号或者节点XmlPath</param>
        /// <param name="nodeType">0代表段落1代表要素</param>
        /// <returns>配置信息数据</returns>
        public string GetExtractInfoData(string nodeName, Int32 nodeType)
        {
            string sql = string.Empty;

            if (nodeType == 2)
            {
                sql = string.Format(@"   select EXI_ID from dbo.ExtractInfo  where EXI_ParagraCode = '{0}' and EXI_DataMode = 2 ", nodeName);
            }
            else if (nodeType == 1)
            {
                sql = string.Format(@" select EXI_ID  from dbo.ExtractInfo where EXI_XmlPath = '{0}' and EXI_DataMode = 1 ", nodeName);
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            object o = oBase.ExecuteScalar(oCmd);
            return o == null?"": o.ToString();             
        }

        /// <summary>
        /// 获取模块配置信息
        /// </summary>
        /// <param name="nodeName">段落编号或者节点XmlPath</param>
        /// <param name="nodeType">0代表段落1代表要素</param>
        /// <returns>配置信息数据</returns>
        public DataTable GetModuleCodeData(string nodeName, Int32 nodeType,string moduleName)
        {
            string sql = string.Empty;
            if (nodeType == 0)
            {
                sql = string.Format(@"   select distinct b.EF_ModuleCode
  from  dbo.ExtractInfo a join dbo.ExtractFeature b on a.EXI_ID=b.EXI_ID  
  where EXI_ParagraCode='{0}' and EXI_DataMode=2 and b.EF_ModuleCode<>'{1}' ", nodeName, moduleName);
            }
            else if (nodeType == 1)
            {
                sql = string.Format(@" select distinct b.EF_ModuleCode
  from  dbo.ExtractInfo a join dbo.ExtractFeature b on a.EXI_ID=b.EXI_ID  
  where EXI_XmlPath='{0}' and EXI_DataMode=1 and b.EF_ModuleCode<>'{1}' ", nodeName, moduleName);
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 根据查询条件获取需要查找的节点
        /// </summary>
        /// <param name="type">0代表段落1代表要素</param>
        /// <param name="condition">efid</param>
        /// <returns>根据efid返回节点树上的节点</returns>
        public string GetDataBySearchCondition(int type,string condition)
        {
            string res = string.Empty;
            if (type == 0)
            {
                res = SqlManage.sqlServerCommonManage.Select<ExtractInfo, ExtractFeature>().InnerJoin((a, b) => a.EXI_ID == b.EXI_ID).Where((a, b) => a.EXI_DataMode == 2 && b.EF_ID == condition)?.First()?.EXI_ParagraCode;
            }
            else if (type == 1)
            {
                res = SqlManage.sqlServerCommonManage.Select<ExtractInfo, ExtractFeature>().InnerJoin((a, b) => a.EXI_ID == b.EXI_ID).Where((a, b) => a.EXI_DataMode == 1 && b.EF_ID == condition)?.First()?.EXI_XmlPath;
            }
            return res;
        }
    }
}
